package com.spring.security.springsecuritydemo.handle;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义登录成功的处理器
 */
public class MySuccessHandle implements AuthenticationSuccessHandler {

    private String url;

    public MySuccessHandle(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        User principal = (User)authentication.getPrincipal();
        System.out.println("用户名:"+principal.getUsername());
        System.out.println("密码"+principal.getPassword());
        System.out.println("权限"+principal.getAuthorities());

        httpServletResponse.sendRedirect(url);
    }
}
